package com.lzb.common.easyui.datagrid;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *EasyuiDataGridDto是获得action中的请求，把page(pageNo)和rows(pageSize)拿到, orders 排序{字段, "DESC" or "ASC"}
 * 
 * 接受参数：page , rows , orders 
 * 
 */
public class DataGridDTO {

	private static final Logger logger = LoggerFactory.getLogger(DataGridDTO.class);
	
	private int page = DataGridConstValues.PAGE_NO_KEY_VALUE_DEFAULT;// 当前页
	private int rows = DataGridConstValues.PAGE_SIZE_KEY_VALUE_DEFAULT;// 每页显示记录数
	private int startIndex = (page - 1) * rows;
	private LinkedHashMap<String, String> orders = new LinkedHashMap<String, String>();// 排序字段名
	
	/**
	 * @param page 页码
	 * @param rows 页行数
	 * @param orders 排序{字段, "DESC" or "ASC"}
	 */
	private DataGridDTO() {}
	
	public static DataGridDTO newInstance(int page, int rows, LinkedHashMap<String, String> orders) {
		DataGridDTO _bean = new DataGridDTO();
		_bean.page = page;
		_bean.rows = rows;
		_bean.orders = orders;
		_bean.startIndex = (page - 1) * rows;
		return _bean;
	}
	
	public static DataGridDTO newInstance(ServletRequest request) {
		DataGridDTO _bean = new DataGridDTO();
		if(request == null) {
			return _bean;
		}
		try {
			String rowsString = request.getParameter(DataGridConstValues.PAGE_SIZE_KEY_NAME);
			if(rowsString != null && !"".equals(rowsString.trim())) {
				_bean.rows =  Integer.parseInt(rowsString);
			}
			String pageNoString = request.getParameter(DataGridConstValues.PAGE_NO_KEY_NAME);
			if(pageNoString != null && !"".equals(pageNoString.trim())) {
				_bean.page =  Integer.parseInt(pageNoString);
			}
			_bean.startIndex = (_bean.page - 1) * _bean.rows;
			String[] keys = request.getParameterValues(DataGridConstValues.ORDER_FIELDS);
			String[] values = request.getParameterValues(DataGridConstValues.ORDER_VALUES);
			int keyLengths = keys.length;
			int valueLengths = values.length;
			if(keys != null && values != null && keyLengths != 0 && valueLengths != 0) {
				if(keyLengths == valueLengths) {
					for(int i=0; i<keyLengths; i++) {
						_bean.orders.put(keys[i], values[i]);
					}
				}
			}
		} catch(Exception e) {
			logger.error("构造DataGridDTO出错.", e);
		} 
		return _bean;
	}
	
	public int getPage() {
		return page;
	}
	public int getRows() {
		return rows;
	}
	public Map<String, String> getOrders() {
		return orders;
	}
	public int getStartIndex() {
		return startIndex;
	}
	
	/**
	 * 只能通过构造函数设置
	 * @param page
	 */
	@Deprecated
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * 只能通过构造函数设置
	 * @param rows
	 */
	@Deprecated
	public void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * 只能通过构造函数设置
	 * @param startIndex
	 */
	@Deprecated
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	/**
	 * 只能通过构造函数设置
	 * @param orders
	 */
	@Deprecated
	public void setOrders(LinkedHashMap<String, String> orders) {
		this.orders = orders;
	}
}
